<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Users_Model extends CI_Model {
    
    var $table = 'users';
    function __construct(){
        parent::__construct();
    }
    
    function CheckLogin($username, $password){
        $where = array('username' => $username, 'password' => $password, 'status' => 1 );
        $this->db->select()->from($this->table)->where($where);
        $result = $this->db->get();
        return $result->row_array();
    }
    /**
     * 
     * @public
     * @param $where is array or string
     * @return array() format key/value and depend $isRow
     * $where -----------------------------------
     * 1/ $where = name = 'Joe' AND title = 'boss' AND status = 'active'
     * 2/ $where = name != 'Joe' AND id < 45 
     * 3/ $where =  array('name' => $name, 'title' => $title, 'status' => $status);
     * 4/ $where = array('name !=' => $name, 'id <' => $id, 'date >' => $date);
     * 5/ $where = "name='Joe' AND status='boss' OR status='active'"; 
     * 
     * $order ---------------------------------
     * 1/ $order = 'title desc'
     * 2/ $order = 'title desc, name asc' 
     * 
     * $limit ---------------------------------
     * 1/ $limit = 10 => LIMIT 0, 10
     * 2/ $limit = '20, 10' => LIMIT 20, 10
     * 
     * $columns -------------------------------
     * 1/ $columns = 'col1, col2, col3, ...'
     * 2/ $columns = 'col1 AS c1, COUNT(col2) AS c2'
     * 
     */
    function Select_($where, $isRow = true, $limit = '', $order = '', $colums = '*'){
        $this->db->select($colums);
        $this->db->from($this->table);
        $this->db->where($where);
        !empty($order) ? $this->db->order_by($order) : '';
        !empty($limit) ? $this->db->limit($limit) : '';
        $query = $this->db->get();
        if($isRow) $result = $query->row_array();
        else $result = $query->result_array();
        return $result;
    }
    /**
     * 
     * @public
     * @param $where is array or string
     * @return number with function num_rows()
     * 1/ $where = name = 'Joe' AND title = 'boss' AND status = 'active'
     * 2/ $where = name != 'Joe' AND id < 45 
     * 3/ $where =  array('name' => $name, 'title' => $title, 'status' => $status);
     * 4/ $where = array('name !=' => $name, 'id <' => $id, 'date >' => $date);
     * 5/ $where = "name='Joe' AND status='boss' OR status='active'"; 
     * 
     */
    function CheckUnique($where){
        $this->db->select();
        $this->db->from($this->table);
        $this->db->where($where);
        $query = $this->db->get();
        return $query->num_rows();
    }
    function Update_($data, $where){
        
    }
    function Insert_($data){
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
/* ============================================

    END CLASS
    

=============================================== */
}